// pages/film-ZX/film-ZX.js
const app = getApp();
const db = wx.cloud.database().collection("groupnews");
const db2 = wx.cloud.database().collection("user");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: '',
    imageList: [], // 用于存放查询到的图片列表
    enable: false,
    actionText: '',
    loadingProps: {
      size: '50rpx',
    },
    searchResult: [] // 查询结果
  },
  onLoad: function () {
    // 在页面加载时调用函数进行数据库查询
    this.getImageList();
  },

  handleClick(e) {
    console.log('User openid:', app.globalData.user_openid);
    
    if (app.globalData.user_openid === '') {
      wx.switchTab({
        url: '/pages/my/my'
      });
      return;
    }
    
    // Query the user collection for the authen field
    db2.where({
      _openid: app.globalData.user_openid,
    }).get().then(res => {  
      if (res.data.length === 0) {
        console.error('No user found with the provided openid');
        wx.showToast({
          title: '获取用户信息失败',
          icon: 'none'
        });
        return;
      }
      
      const authen = res.data[0].authen;
      console.log('User authen field:', authen);
      
      if (authen === 2) {
        wx.navigateTo({
          url: '/pages/film-ZX-up/film-ZX-up'
        });
      } else {
        wx.switchTab({
          url: '/pages/my/my'
        });
        wx.showToast({
          title: '请完成身份认证',
          icon: 'error'
        });
      }
    }).catch(err => {
      console.error('Failed to get user authen:', err);
      wx.showToast({
        title: '获取用户信息失败',
        icon: 'none'
      });
    });
  },
  

  showImage(e) {
    const datasetUrl = e.currentTarget.dataset.url;
    console.log("datasetUrl:", datasetUrl);
    // Ensure that the datasetUrl is not undefined or empty
    if (datasetUrl) {
      wx.navigateTo({
        url: '/pages/film-ZX-detail/film-ZX-detail?url=' + datasetUrl
      });
    } else {
      console.error("Image URL is undefined or empty.");
    }
  },
  getImageList: function () {
    const db = wx.cloud.database();
    db.collection("groupnews").get().then(res => { 
      console.log(res.data)
      this.setData({
        imageList: res.data
      });
    }).catch(err => {
      console.error('Failed to get image list:', err);
    });
  }
  ,
  ready() {
    this.setData({ enable: true });
    setTimeout(() => {
      this.setData({ enable: false });
    }, 1000);
  },
  onRefresh() {
    this.setData({ enable: true });
    this.getImageList();
    setTimeout(() => {
      this.setData({ enable: false });
    }, 1500); 
  }
  ,
  onScroll(e) {
    const { scrollTop } = e.detail;
    this.setData({ scrollTop });
  },
  
  // 执行搜索
  search() {
    this.setData({
      actionText: '',
    });
    this.hideSearchResult()
    const keyword = this.data.value.trim(); // 获取用户输入的内容并去除两端空格
    console.log(keyword)
    if (!keyword) {
      wx.showToast({
        title: '请输入搜索内容',
        icon: 'none'
      });
      return;
    }

    // 执行模糊查询
    db.where({
      // title: db.RegExp({
      //   regexp: keyword, // 正则表达式，用于模糊查询
      //   options: 'i' // 'i' 表示忽略大小写
      // })
      // title:keyword
      title:this.data.value
    }).get().then(res => {
      if (res.data.length === 0) {
        // Display message for no results found
        wx.showToast({
          title: '查询不到结果',
          icon: 'error'
        });
      } else {
      this.setData({
        searchResult: res.data // 更新查询结果
      });
    }
    }).catch(err => {
      console.error('查询失败', err);
      wx.showToast({
        title: '查询失败，请重试',
        icon: 'none'
      });
    });
  },


    focusHandle() {
      this.setData({
        actionText: '搜索',
      });
    },

    blurHandle() {
      this.setData({
        actionText: '',
      });
    },

    actionHandle() {
      this.search()
      
    },
    hideSearchResult() {
      this.setData({
        searchResult: [] 
      });
    }
    
})